﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.Aparts.Web.Model;
using blackhouse.Aparts.Web.Presenter;
using blackhouse.Aparts.Web.View;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Business.Opinions;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Web.Navigation;
using blackhouse.JavaScript.JQuery;
using blackhouse.mojoPortalExtend.Breadcrumbs;
using blackhouse.Web.Controls;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using System.Text;
using blackhouse.mojoPortalExtend;

namespace blackhouse.StareKino.Web {
    public partial class OpinionDetails : NonCmsBasePage, IOpinionDetailsView {

        #region Fields

        private int fOpinionId = -1;
        private OpinionDetailsPresenter presenter;

        #endregion

        #region Properties

        public int OpinionId {
            get {
                if (this.fOpinionId == -1)
                    this.fOpinionId = this.GetOpinionId();
                return this.fOpinionId;
            }
        }

        public double AverageRating {
            get;
            set;
        }

        public double SimilarAverageRating {
            get;
            set;
        }

        public bool PublishButtonVisible {
            get {
                return this.lnkPublish.Visible;
            }
            set {
                this.lnkPublish.Visible = value;
            }
        }

        public List<IOpinion> SimilarOpinions {
            get {
                return this.rptOpinions.DataSource as List<IOpinion>;
            }
            set {
                this.rptOpinions.DataSource = value;
            }
        }

        public IDictionary<string, double> CategoryValues {
            get {
                return this.ctrlOpinionCategories.Categories;
            }
            set {
                this.ctrlOpinionCategories.Categories = value;
            }
        }

        private bool haveOpinionAdminPriv;
        private bool haveOpinionAdminPrivSetted;

        public bool HaveOpinionAdminPriv {
            get {
                if (!this.haveOpinionAdminPrivSetted) {
                    this.haveOpinionAdminPriv = WebUser.IsAdminOrContentAdminOrContentPublisher || WebUser.IsInRole("Opinions");
                    this.haveOpinionAdminPrivSetted = true;
                }
                return this.haveOpinionAdminPriv;
            }
        }

        #endregion

        #region Methods

        public int GetUserId() {
            if (User.Identity.IsAuthenticated)
                return SiteUtils.GetCurrentSiteUser().UserId;
            else return 0;
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.AddClassToBody("short-top");
            this.presenter = new OpinionDetailsPresenter(this, new ApartsOpinionsModel());
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.presenter.LoadView();

            if (this.RequestHtmlSnapshot()) {
                this.presenter.LoadSimilarOpinions();
            }
            else {
                this.rptOpinions.Visible = false;
                this.pnlOpinionsLoad.Visible = true;
                this.SupportAjaxContent();
            }
            this.ctrlOpinionCategories.AddOpinionNumber(this.AverageRating, "wartość opinii", "ownAvgScore", String.Empty);
            this.ctrlOpinionCategories.AddOpinionNumber(this.SimilarAverageRating, "średnia wartość opinii dla tego apartamentu", "similarAvgScore", String.Empty);
            this.DataBind();
            this.AdditionalMetaMarkup = "<meta name=\"fragment\" content=\"!\">";
        }

        private void SetSeo(DisplayOpinion displayOpinion) {
            string title = String.Format("Opinia klienta {0} {1} wystawiona {2:D}", displayOpinion.Publisher.FirstName, displayOpinion.Publisher.LastName, displayOpinion.PublishedDate);
            string url = Request.Url.GetLeftPart(UriPartial.Authority) + displayOpinion.LinkToOpinion;
            string description = title + ". Opinia wystawiona do apartamentu " + displayOpinion.GetAssociatedApartmentGroup().Name;

            PageSeo pageSeo = new PageSeo(title, description, url);
            PageProvider pp = new PageProvider();
            pp.SetSeo(pageSeo);
            //NavigationProvider nav = NavigationProvider.Instance();
            //string url = nav.GetUrl(displayOpinion.opinion);

            //string name = displayOpinion.Publisher.FirstName;
            //if (!string.IsNullOrEmpty(displayOpinion.Publisher.LastName)) {
            //    name += " ";
            //    name += displayOpinion.Publisher.LastName;
            //}
            //name += ", ";
            //name += displayOpinion.Country;
            //name += " - ";
            //name += displayOpinion.PublishedDateFormatted;

            //string name2 = "Opinia do apartamentu " + displayOpinion.ApartmentText + " | " + name;

            //this.PageProvider.SetPageTitle(name2);
            //this.PageProvider.SetPageDescription(name2);
            //this.PageProvider.SetCanonicalUrl(url);

            //if (Request.UrlReferrer != null) {
            //    this.lnkBacklinkToOpinion.Visible = false;
            //    this.lnkBacklink.NavigateUrl = Request.UrlReferrer.ToString();
            //}
            //else {
            //    this.lnkBacklink.Visible = false;
            //    this.lnkBacklinkToOpinion.NavigateUrl = opinionUrl;
            //}

        }

        private bool RequestHtmlSnapshot() {
            return Request.QueryString.AllKeys.Contains("_escaped_fragment_");
        }

        private void SupportAjaxContent() {
            this.opinionId.Value = this.OpinionId.ToString();
            JQueryLibraries jql = new JQueryLibraries();
            jql.IncludeJQueryTemplates();
            jql.IncludeJQueryWaypoints();

#if DEBUG
            jql.IncludeFileBasedOnJQuery("blackhouse.StareKino.Web.ClientScripts.opinion-details.js,blackhouse.StareKino.Web");
#else
            jql.IncludeFileBasedOnJQuery("blackhouse.StareKino.Web.ClientScripts.opinion-details.min.js,blackhouse.StareKino.Web");
#endif
            this.pnlSimilarOpinions.Attributes.Add("data-panel", "similaropinions");
            this.pnlOpinionsLoad.Attributes.Add("data-panel", "opinionsload");

            blackhouse.Web.Controls.TemplateControl.TemplateControl tplCtrl = new blackhouse.Web.Controls.TemplateControl.TemplateControl();
            tplCtrl.TemplateId = "opinion-item";
            tplCtrl.Controls.Add(Page.LoadControl("~/Modules/OpinionItem.ascx"));
            this.Page.Header.Controls.Add(tplCtrl);
        }

        public override void DataBind() {
            if (this.rptOpinions.DataSource != null)
                this.rptOpinions.DataBind();

            base.DataBind();
        }

        private int GetOpinionId() {
            int rOpinionId = 0;
            string sOpinionId = Request.QueryString["opinion"];
            if (!string.IsNullOrEmpty(sOpinionId) && Int32.TryParse(sOpinionId, out rOpinionId))
                return rOpinionId;
            else
                return 0;
        }

        public void ShowNoOpinionInfo() {
            this.pnlNoOpinion.Visible = false;
            this.pnlNoOpinion.Visible = true;
        }

        public void DisplayOpinion(DisplayOpinion displayOpinion) {
            this.ltlPublisherName.Text = displayOpinion.Publisher.FirstName + " " + displayOpinion.Publisher.LastName + ", " + displayOpinion.Country;
            this.ltlPublishDate.Text = displayOpinion.PublishedDateFormatted;
            this.lblInPlus.Text = displayOpinion.InPlusOpinionFormatted;
            this.lblInMinus.Text = displayOpinion.InMinusOpinionFormatted;

            if (displayOpinion.HaveAnswer) {
                this.pnlAnswer.Visible = true;
                this.lblAnswer.Text = displayOpinion.Answer;
            }

            this.SetSeo(displayOpinion);
        }

        public void HideSimilarOpinions() {
            this.pnlSimilarOpinions.Visible = false;
        }

        public void DisplayAssociatedApartment(IApartmentGroup group) {
            this.apartmentLine.PopulateControl(group);
        }

        public void EnableEditMode() {
            this.ReplaceToEdit(this.lblInPlus, this.txtInPlus);
            this.ReplaceToEdit(this.lblInMinus, this.txtInMinus);
            this.ReplaceToEdit(this.lblAnswer, this.txtAnswer);
            this.pnlAnswer.Visible = true;
            this.pnlActions.Visible = true;
        }

        private void ReplaceToEdit(Literal literal, TextBox txt) {
            txt.Text = literal.Text;
            txt.Visible = true;
            literal.Visible = false;
        }

        protected void lnkPublish_Click(object sender, EventArgs e) {
            this.SaveOpinion(true);
        }

        public void ShowPublishedInfo() {
            this.notifPublished.Visible = true;
        }

        protected void lnkSave_Click(object sender, EventArgs e) {
            this.SaveOpinion(false);
        }

        private void SaveOpinion(bool withPublish) {
            string inPlus = Request[this.txtInPlus.UniqueID];
            string inMinus = Request[this.txtInMinus.UniqueID];
            string answer = Request[this.txtAnswer.UniqueID];
            this.presenter.SaveOpinion(inPlus, inMinus, answer, withPublish);
        }

        public void ShowSavedInfo(bool status) {
            this.notifSaveSuccess.Visible = status;
            this.notifErrorSave.Visible = !status;
        }

        #endregion

    }
}